#include "Ogre7zPlugin.h"

#include "Ogre7zArchiveFactory.h"
#include "Ogre7zCommon.h"

#include <OgreException.h>
#include <OgreArchiveManager.h>



namespace Ogre
{

//-------------------------------------------------------------------------------------------------------------------------------------------------
Arcive7zPlugin::Arcive7zPlugin()
: mSevenZipArchiveFactory(NULL)
{
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
Arcive7zPlugin::~Arcive7zPlugin()
{
	OgreAssert( !mSevenZipArchiveFactory, "Application bug. " 
		"The factory for 7zip archive must be destroyed "
		"before Arcive7zPlugin." );
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
const String& Arcive7zPlugin::getName() const
{
	static const String sPluginName = "7zip Archive";
	return sPluginName;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void Arcive7zPlugin::install()
{
	OgreAssert( !mSevenZipArchiveFactory, "Application bug. 7zip plugin is already installed." );

	// Initialise 7zip library
	SevenZipUtil::initialiseLibrary();

	// Create new factory instance
	mSevenZipArchiveFactory = OGRE_NEW_T( SevenZipArchiveFactory, MEMCATEGORY_GENERAL )();

	// Register 7zip archive factory
	ArchiveManager::getSingleton().addArchiveFactory( mSevenZipArchiveFactory );
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void Arcive7zPlugin::initialise()
{
	assert( mSevenZipArchiveFactory && "Application bug. 7zip plugin not installed." );

	// Is factory instance exist?
	if( !mSevenZipArchiveFactory )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"Arcive7zPlugin::initialise" );
	}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void Arcive7zPlugin::shutdown()
{
	assert( mSevenZipArchiveFactory && "Application bug. 7zip plugin not installed." );

	// Is factory instance exist?
	if( !mSevenZipArchiveFactory )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"Arcive7zPlugin::initialise" );
	}
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void Arcive7zPlugin::uninstall()
{
	OgreAssert( mSevenZipArchiveFactory, "Application bug. 7zip plugin not installed." );

	// Destroy existing instance
	OGRE_DELETE_T( mSevenZipArchiveFactory, SevenZipArchiveFactory, MEMCATEGORY_GENERAL );
	mSevenZipArchiveFactory = NULL;
}

} // end of namespace Ogre
